<HTML><HEAD><TITLE>open(++SourceSink, +Mode, ?Stream, ++Options)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Stream I/O</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>open(++SourceSink, +Mode, ?Stream, ++Options)</H1>
Opens the I/O source or sink SourceSink in mode Mode and associates it
with the stream identifier Stream.


<DL>
<DT><EM>SourceSink</EM></DT>
<DD>Atom, string or structure.
</DD>
<DT><EM>Mode</EM></DT>
<DD>One of the atoms read, write, update, append.
</DD>
<DT><EM>Stream</EM></DT>
<DD>Atom or variable.
</DD>
<DT><EM>Options</EM></DT>
<DD>List.
</DD>
</DL>
<H2>Description</H2>
   This predicate opens an ECLiPSe I/O stream.

<P>
   The most common use is for opening files. In this case, SourceSink
   is a file name (atom or string).

<P>
   Mode is one of the following

<P>
<PRE>
    read         open for reading
    write        open for writing
    update       open for reading and writing
    append       open for writing at the end
</PRE>
   A file must already exist if it is to be opened in read mode.  A file
   opened in append mode is opened in write mode at the end of the file.
   If an existing file is opened in write mode, it is truncated to zero
   size, i.e. its previous content is lost.

<P>
   Stream is the identifier that is subsequently used to identify the
   stream.  This identifier is either a name (atom) provided by the user,
   or a system-generated identifier (when the user passes a free variable).

<P>
   If SourceSink is of the form string(InitialString), then a so-called
   string stream is opened.  A string stream is basically an in-memory
   file and its initial contents is the string InitialString.
   A string stream can be used like any other stream, i.e. it is possible
   to read, write and seek like on a true file.
   The current contents of a string stream can at any time be retrieved
   as a whole using get_stream_info(Stream, name, Contents).

<P>
   If SourceSink is of the form queue(InitialString), then a queue
   stream is opened. It behaves like a string that can be written at the
   end and read from the beginning.  Seeking is not allowed on queues.
   The current contents of a queue can at any time be retrieved as a
   whole using get_stream_info(Stream, name, Contents). Queues are
   considered to be at end-of-file while they are empty. 
   Queues can be configured to raise an event every time something
   is written to the previously empty queue (see open/4).

<P>
   If SourceSink is of the form fd(Integer), then the stream in opened
   onto an existing operating system file descriptor.

<P>
   Options is a list of the following stream options:
<DL>
<DT><STRONG>alias(Name)</STRONG><DD>
    Make the stream known under an alternative name.
    Name is an atom. See also set_stream/2.

<DT><STRONG>compress(OnOff)</STRONG><DD>
    a hint for output operations (e.g. write_exdr/2) to use a more
    compact output format (output streams only). OnOff is one of the
    atoms on or off. The default is on (for files, pipes and sockets)
    or off (for queues and string streams).

<DT><STRONG>end_of_line(CrLf)</STRONG><DD>
    This option affects only write-streams and determines which
    end-of-line character sequence is written by predicates like nl/1,
    writeln/1 and printf/3.  Possible values are the atoms lf and
    crlf.  The default for string and queue streams is lf, for other
    streams it is operating-system dependent.

<DT><STRONG>event(Name)</STRONG><DD>
    This option configures a read-queue stream or a socket stream to raise
    the given event whenever data arrives on the previously empty stream.
    This option is intended for queue streams in embedded applications
    of ECLiPSe, or for socket streams in remote connection setups.  The
    event handler should consume all data that is available on the stream.
    Name must be an atom or an event handle.

<DT><STRONG>flush(Where)</STRONG><DD>
    This option affects only write-streams and allows to configure a
    stream to automatically flush after every line written.  Where is
    one of the atoms end_of_line (flush automatically after every
    line) or flush (require explicit flushing).  The default setting
    is flush, except for tty streams where the default is end_of_line.

<DT><STRONG>macro_expansion(OnOff)</STRONG><DD>
    Input streams only. Specifies whether term-macros (see macro/3) will
    be expanded when reading from this stream using read/2, read_term/3 etc.
    OnOff is one of the atoms on or off, the default is on.

<DT><STRONG>output_options(OptionList)</STRONG><DD>
    Write-streams only. Specifies the default output options that will be
    used when printing terms onto this stream, e.g. using write/2.
    The format of OptionList is the same as the one accepted by the
    write_term/2,3 predicates.
    Note that certain output predicates can override these default
    settings, e.g. writeq/2, write_term/3, printf/3, etc.

<DT><STRONG>yield(OnOff)</STRONG><DD>
    This option is intended for queue streams in embedded applications
    of ECLiPSe.  It configures the stream to yield control to the host
    program whenever a read-queue reaches end-of-file or a write-queue
    gets flushed.  See the Embedding Manual for more details.  OnOff
    is one of the atoms on or off.

</DL>
   Note that streams are not closed on backtracking through the call to open/3.

<P>

<H3>Modes and Determinism</H3><UL>
<LI>open(++, +, -, ++) is det
<LI>open(++, +, +, ++) is det
</UL>
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>File or Mode is not instantiated.
<DT><EM>(5) type error </EM>
<DD>File is not an atom, string or structure.
<DT><EM>(5) type error </EM>
<DD>Mode is not an atom.
<DT><EM>(5) type error </EM>
<DD>Stream is not an atom or a variable.
<DT><EM>(170) system interface error </EM>
<DD>The operating system cannot open the file.
<DT><EM>(192) illegal stream mode </EM>
<DD>Mode is an atom, but is not a valid mode.
</DL>
<H2>Examples</H2>
<PRE>
    See open/3.



</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/iostream/set_stream_property-3.html">set_stream_property / 3</A>, <A HREF="../../kernel/iostream/at-2.html">at / 2</A>, <A HREF="../../kernel/iostream/at_eof-1.html">at_eof / 1</A>, <A HREF="../../kernel/iostream/current_stream-1.html">current_stream / 1</A>, <A HREF="../../kernel/iostream/get_stream_info-3.html">get_stream_info / 3</A>, <A HREF="../../kernel/iostream/open-3.html">open / 3</A>, <A HREF="../../kernel/iostream/pipe-2.html">pipe / 2</A>, <A HREF="../../kernel/iostream/seek-2.html">seek / 2</A>, <A HREF="../../kernel/iostream/select-3.html">select / 3</A>, <A HREF="../../kernel/iostream/stream_truncate-1.html">stream_truncate / 1</A>, <A HREF="../../kernel/iostream/flush-1.html">flush / 1</A>, <A HREF="../../kernel/ioterm/write_term-2.html">write_term / 2</A>, <A HREF="../../kernel/ioterm/write_term-3.html">write_term / 3</A>
</BODY></HTML>
